//
//  JCSeekPageViewController.m
//  ABookOfCartoon
//
//  Created by 美国大狗 on 15-5-13.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "JCSeekPageViewController.h"
#import "JCSeekCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "JCSeekPageSEEKViewController.h"
#import "JCCataViewController.h"
#import "MBProgressHUD.h"

@interface JCSeekPageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate>
@property (nonatomic, retain)UITextField *textField;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation JCSeekPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"搜索";
    self.navigationController.navigationBar.translucent = NO;
    
    // 小菊花
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = YES;
    
    //设置对话框文字
    HUD.labelText = @"请稍等";
    
    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);   // 数字是显示的时间
    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];
        [HUD release];
        MBProgressHUD  *HUD = nil;
        // 又开始一遍 后台执行并显示出来, 在加载真正视图之前
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        HUD.labelText = @"加载成功";
        HUD.mode = 	MBProgressHUDModeCustomView // 类型
        ;
        // 加载完成后显示了一个图片视图 时间为 5秒
        HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"/Users/dlios/ABookOfCartoon/ABookOfCartoon/aihua.png"]] autorelease];
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(1); // 时间
        } completionBlock:^{
            [HUD removeFromSuperview];
            [HUD release];
        }];
        
    }];
 

    
    
//    //******************小菊花?***************************
//    //创建齿轮并且改变齿轮大小
//    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//    //指定齿轮中心
//    [act setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2 - 100)];
//    //设置进度轮显示类型(Style)
//    act.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
//    //设置停止状态
//    act.hidesWhenStopped = NO;
//    //显示
//    [self.view addSubview:act];
//    //内存管理
//    [act release];
//    //***************************************************
    [self getDataFromWeb];
    
    }


//创建瀑布流效果的collectionView,直接self调用
- (void)createCollectionView
{
    UICollectionViewFlowLayout *f = [[UICollectionViewFlowLayout alloc] init];
    f.itemSize = CGSizeMake(100, 180);
    f.minimumLineSpacing = 10;
    f.minimumInteritemSpacing = 3;
    f.sectionInset = UIEdgeInsetsMake(145, 30, 5, 30);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:f];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView release];
    [f release];
    //以下为注册cell
    [collectionView registerClass:[JCSeekCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //预留上方搜索栏:
    collectionView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
    

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 255 )/ 2, 75, 200, 40)];
    [_textField.layer setCornerRadius:5];
    [_textField.layer setBorderWidth:1];
    [_textField.layer setBorderColor:[UIColor grayColor].CGColor];
    _textField.clearsOnBeginEditing = YES;
    _textField.delegate = self;
    _textField.borderStyle = UITextBorderStyleBezel;
    [collectionView addSubview:_textField];
    [_textField release];
    
    UIButton *seek = [UIButton buttonWithType:UIButtonTypeSystem];
    seek.frame = CGRectMake(self.textField.frame.size.width + (self.view.frame.size.width - 255 )/ 2, 75, 40, 40);
    [seek.layer setCornerRadius:5];
    [seek.layer setBorderWidth:1];
    [seek.layer setBorderColor:[UIColor grayColor].CGColor];
    [seek setTitle:@"搜索" forState:UIControlStateNormal];
    [seek addTarget:self action:@selector(seek) forControlEvents:UIControlEventTouchUpInside];
    [collectionView addSubview:seek];
}

//以下为collectionView实现方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JCSeekCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    JCSeekPageModel *mo = [[JCSeekPageModel alloc] init];
//************************************
    mo.CatalogName = [[_dataArr objectAtIndex:indexPath.item]CatalogName];
    mo.CatalogOtherURL = [[_dataArr objectAtIndex:indexPath.item] CatalogOtherURL];
    
//************************************
    cell.model = mo;
    return cell;
}
//点击方法:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JCCataViewController *sb = [[JCCataViewController alloc] init];
    sb.EXXtitle = [_dataArr[indexPath.item]CatalogName];
    if (indexPath.row == 0) {
        sb.cataID = 72;//完结
    } else if (indexPath.row == 1){
        sb.cataID = 53;//少年热血
    } else if (indexPath.row == 2){
        sb.cataID = 65;//少女爱情(FFF团参上!)
    } else if (indexPath.row == 3){
        sb.cataID = 74;//校园漫画
    } else if (indexPath.row == 4){
        sb.cataID = 56;//竞技体育
    } else if (indexPath.row == 5){
        sb.cataID = 54;//武侠格斗
    } else if (indexPath.row == 6){
        sb.cataID = 55;//科幻魔幻
    } else if (indexPath.row == 7){
        sb.cataID = 73;//东方神鬼
    } else if (indexPath.row == 8){
        sb.cataID = 58;//侦探推理
    } else if (indexPath.row == 9){
        sb.cataID = 59;//恐怖悬疑
    } else if (indexPath.row == 10){
        sb.cataID = 57;//爆笑喜剧
    } else if (indexPath.row == 11){
        sb.cataID = 78;//美食漫画
    } else if (indexPath.row == 12){
        sb.cataID = 68;//百合
    } else if (indexPath.row == 13){
        sb.cataID = 69;//后宫
    } else if (indexPath.row == 14){
        sb.cataID = 76;//生活亲情
    } else if (indexPath.row == 15){
        sb.cataID = 77;//治愈系
    } else if (indexPath.row == 16){
        sb.cataID = 79;//萌系
    } else if (indexPath.row == 17){
        sb.cataID = 60;//大陆漫画
    } else if (indexPath.row == 18){
        sb.cataID = 61;//日本漫画
    } else if (indexPath.row == 19){
        sb.cataID = 81;//韩国漫画
    } else if (indexPath.row == 20){
        sb.cataID = 62;//港台漫画
    } else if (indexPath.row == 21){
        sb.cataID = 63;//欧美漫画
    } else if (indexPath.row == 22){
        sb.cataID = 67;//漫画期刊
    } else if (indexPath.row == 23){
        sb.cataID = 66;//福利社
    } else if (indexPath.row == 24){
        sb.cataID = 70;//暴走漫画
    } else if (indexPath.row == 25){
        sb.cataID = 71;//爆笑囧图
    }
    sb.pageID = 0;
    [self.navigationController pushViewController:sb animated:YES];
}

- (void)seek
{
    JCSeekPageSEEKViewController *sb = [[JCSeekPageSEEKViewController alloc] init];
    if (self.textField.text.length != 0) {
        sb.StrOfSeek = self.textField.text;
        [self.navigationController pushViewController:sb animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"亲爱的宅男:" message:@"你还没有输入搜索内容哦!亲!QwQ" delegate:self cancelButtonTitle:@"额,我忘了" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)getDataFromWeb
{
    //网址 -> string
    NSString *str = @"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=catalogs";
    //string -> url
    NSURL *url = [NSURL URLWithString:str];
    //url -> request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dataArr = [[NSMutableArray array] init];
        NSArray *arr = result;
        for (NSDictionary *dic in arr) {
            JCSeekPageModel *m = [[JCSeekPageModel alloc] initWithDic:dic];
            [_dataArr addObject:m];
            [m release];
            NSLog(@"%@",m);
        }
//        NSLog(@"%@",_dataArr);
        [self createCollectionView];
    }];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    NSLog(@"%s", __FUNCTION__);
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
